package com.omaitec.log4j.mq.expand;

import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQObjectMessage;

public class Log4jConsumer1 extends Thread {

    MessageConsumer consumer;

    public Log4jConsumer1() throws Exception {
	ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
		"tcp://localhost:61616");
	Connection conn = factory.createConnection();
	Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
	conn.start();
	consumer = sess.createConsumer(sess.createQueue("logQueue"));

    }

    public void run() {
	while (true) {
	    try {
		Message message = consumer.receive();
		LoggingEventWrapper event = (LoggingEventWrapper) ((ActiveMQObjectMessage) message)
			.getObject();
		System.out.println("Consumer:" + event.getIpAddress() + ","
			+ event.getMessage());
		Thread.sleep(1000);
	    } catch (Exception e) {
		e.printStackTrace();
	    }

	}
    }
}
